package com.weishuang.monitor.leader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.weishuang.monitor.leader.commons.enums.SCODE;
import com.weishuang.monitor.leader.entity.SysRole;
import com.weishuang.monitor.leader.entity.dto.RoleDTO;
import com.weishuang.monitor.leader.entity.vo.RoleVO;
import com.weishuang.monitor.leader.mapper.SysRoleMapper;
import com.weishuang.monitor.leader.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色信息 服务实现类
 * </p>
 *
 * @author weishuang
 * @since 2021-03-23
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    /**
     * 分页查询角色列表
     * @param page
     * @return
     */
    @Override
    public Page<SysRole> getRolePage(RoleDTO page) {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(page.getRoleName())) {
            queryWrapper.like(SysRole::getRoleName, page.getRoleName());
        }
        queryWrapper.eq(SysRole::getStatus, SCODE.STATUS_NORMAL.getValue());
        queryWrapper.orderByDesc(SysRole::getSort);
        queryWrapper.orderByDesc(SysRole::getCreatedTime);
        page.startPage();
        Page<SysRole> sysRoles = (Page<SysRole>) baseMapper.selectList(queryWrapper);
        return sysRoles;
    }

    /**
     * 查询角色列表与用户已勾选的角色
     * @param userId
     * @return
     */
    @Override
    public List<RoleVO> listRoleAndCheckRoleByUserId(String userId) {
        return baseMapper.listRoleAndCheckRoleByUserId(userId);
    }
}
